{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Transformer with non-trivial phase shift and tap ratio\n",
    "\n",
    "This example is a copy of pandapower's minimal example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypsa\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network = pypsa.Network()\n",
    "\n",
    "network.add(\"Bus\",\"MV bus\",v_nom=20, v_mag_pu_set=1.02)\n",
    "network.add(\"Bus\",\"LV1 bus\",v_nom=.4)\n",
    "network.add(\"Bus\",\"LV2 bus\",v_nom=.4)\n",
    "\n",
    "network.add(\"Transformer\", \"MV-LV trafo\", type=\"0.4 MVA 20/0.4 kV\", bus0=\"MV bus\", bus1=\"LV1 bus\")\n",
    "network.add(\"Line\", \"LV cable\", type=\"NAYY 4x50 SE\",bus0=\"LV1 bus\", bus1=\"LV2 bus\", length=0.1)\n",
    "network.add(\"Generator\", \"External Grid\", bus=\"MV bus\", control=\"Slack\")\n",
    "network.add(\"Load\", \"LV load\", bus=\"LV2 bus\", p_set=0.1, q_set=0.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_pf():\n",
    "    network.lpf()\n",
    "    network.pf(use_seed=True)\n",
    "    return pd.DataFrame({'Voltage Angles': network.buses_t.v_ang.loc['now']*180./np.pi, \n",
    "                        'Volate Magnitude': network.buses_t.v_mag_pu.loc['now']})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "run_pf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.tap_position = 2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.tap_position = -2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now play with tap changer on LV side"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_trafo_lv_tap = network.transformer_types.loc[[\"0.4 MVA 20/0.4 kV\"]]\n",
    "new_trafo_lv_tap.index = [\"New trafo\"]\n",
    "new_trafo_lv_tap.tap_side = 1\n",
    "new_trafo_lv_tap.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformer_types = network.transformer_types.append(new_trafo_lv_tap)\n",
    "network.transformers.type = \"New trafo\"\n",
    "network.transformers.tap_position = 2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.tap_position = -2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now make sure that the phase shift is also there in the LOPF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.generators.p_nom = 1.\n",
    "network.lines.s_nom = 1.\n",
    "network.lopf()\n",
    "pd.DataFrame({'Voltage Angles': network.buses_t.v_ang.loc['now']*180./np.pi, \n",
    "                        'Volate Magnitude': network.buses_t.v_mag_pu.loc['now']})"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
